/********************************************************************************
	firm_analysis.do
	
	This file produces all of the results provided in 
	The Political Resource Blessing or Curse?
	Patronage Networks, Infrastructure Investment, and
	Economic Development in China
	Author: Zhenhuan Lei
	Date:2022/08/25
	Program: Stata 17 MP
	Environment: Win 11
	
*********************************************************************************/

use firm\firm.dta, clear

*********************************Merging Data*********************************
destring postcode province year, replace
replace postcode = int(postcode / 10)
merge m:m postcode using firm\postcode3.dta, keepusing(citycode)
drop if _merge == 2
gen unmatched = 1 if _merge == 1 

*Solving a technical problem in the Chinese postcode 
*这些企业不能被匹配，一般是因为邮政编码最后多了非0的数字。这个数字一般是一个地级市下面的某个县，一般是可以被替换为0，然后再匹配的。
	drop _merge
	drop citycode
	replace postcode = int(postcode/10)*10 if unmatched == 1
	merge m:m postcode using firm\postcode3.dta, keepusing(citycode)
	drop if _merge == 2
	replace unmatched = 0 if _merge == 3
	drop _merge

*Merging patronage connections in 2008 and 2009
	rename citycode cityID
	merge m:m cityID using city_2009.dta
	drop if _merge == 2
	drop _merge

*Merging cities's patronage connections in 2007 for placebo tests later
	merge m:m cityID using city_2007.dta
	drop if _merge == 2
	drop _merge

*Creating primary explanatory variables
	gen treat09 = (treat_mayor09==1 | treat_ps09==1) if !missing(treat_mayor09) | !missing(treat_ps09)
	gen treat07 = (treat_mayor07==1 | treat_ps07==1) if !missing(treat_mayor07) | !missing(treat_ps07)
	gen after = (year > 2007)

*Merging control variables (base year: 2007)
	preserve
		use infrastructure_use.dta, clear
		keep if year == 2009
		save other_09.dta, replace
	restore

	merge m:m cityID using other_09.dta, keepusing(c07* fsj)
	drop if _merge == 2
	drop _merge
	capture erase other_09.dta

* Genrate other variables *

gen lsize = log(employee_num + 1)
gen lrev = log(annual_revenue + 1)
gen sector2 = (main_ind2 == 1 | main_ind3 == 1 | main_ind4 == 1 | main_ind5 == 1)
gen congress = (cppcc_memb | npc_memb)
replace pol_status = . if pol_status<0

global owner_politics affil_ccp npc_memb cppcc_memb
global firm_cont firm_age i.firm_type 

* Table A2 *

su annual_revenue firm_age i.firm_type affil_ccp npc_memb cppcc_memb gender age college pol_status soc_status if sector2
su loan_decrease7 loan_decrease1 loan_decrease2 loan_decrease8 if sector2 & year == 2009

* Table 4 *

	eststo clear
	eststo: qui areg loan_decrease7 treat09 if sector2 & year == 2009, a(province) vce(cluster cityID)
	eststo: qui areg loan_decrease7 treat09 $firm_cont main_ind2 main_ind3 main_ind4 if sector2 & year == 2009, a(province) vce(cluster cityID)
	eststo: qui areg loan_decrease7 treat09 $firm_cont $owner_politics gender age college main_ind2 main_ind3 main_ind4 if sector2 & year == 2009, a(province) vce(cluster cityID)
	eststo: qui areg loan_decrease7 treat09 $firm_cont $owner_politics gender age college main_ind2 main_ind3 main_ind4 c07* if sector2 & year == 2009, a(province) vce(cluster cityID)
	eststo: qui areg loan_decrease7 treat07 $firm_cont $owner_politics gender age college main_ind2 main_ind3 main_ind4 c07* if sector2 & year == 2009, a(province) vce(cluster cityID)
	eststo: qui areg loan_decrease1 treat09 $firm_cont $owner_politics gender age college main_ind2 main_ind3 main_ind4 c07* if sector2 & year == 2009, a(province) vce(cluster cityID)
	eststo: qui areg loan_decrease2 treat09 $firm_cont $owner_politics gender age college main_ind2 main_ind3 main_ind4 c07* if sector2 & year == 2009, a(province) vce(cluster cityID)
	eststo: qui areg loan_decrease8 treat09 $firm_cont $owner_politics gender age college main_ind2 main_ind3 main_ind4 c07* if sector2 & year == 2009, a(province) vce(cluster cityID)
	esttab, se b(3) t(3) scalars(N_clust) star(* 0.1 ** 0.05 *** 0.01) keep(treat09 treat07)
	
* Figure G1 *	
	
	qui areg pol_status 1.treat09##ib2001.year $firm_cont gender age college if sector2, a(cityID) vce(cluster cityID)
	coefplot, keep(1.treat09#*.year) ///
		coeflabels(1.treat09#2003.year = "2003" ///
		1.treat09#2005.year = "2005" ///
		1.treat09#2007.year = "2007" /// 
		1.treat09#2009.year = "2009" /// 
		1.treat09#2011.year = "2011") ///
		levels(90) ///
		vertical ///
		title(A. Political Status (2008-09 connection)) ///
		yline(0, lp(dash)) ///
		addplot(line @b @at) ///
		ciopts(recast(rcap)) ///
		graphregion(color(gs16)) ///
		ylabel(#5, nogrid) ///
		scheme(s2mono)
	graph save temp_09a, replace
	
	qui areg soc_status 1.treat09##ib2001.year $firm_cont gender age college if sector2, a(cityID) vce(cluster cityID)
	coefplot, keep(1.treat09#*.year) ///
		coeflabels(1.treat09#2003.year = "2003" ///
		1.treat09#2005.year = "2005" ///
		1.treat09#2007.year = "2007" /// 
		1.treat09#2009.year = "2009" /// 
		1.treat09#2011.year = "2011") ///
		levels(90) ///
		vertical ///
		title(B. Social Status (2008-09 connection)) ///
		yline(0, lp(dash)) ///
		addplot(line @b @at) ///
		ciopts(recast(rcap)) ///
		graphregion(color(gs16)) ///
		ylabel(#5, nogrid) ///
		scheme(s2mono)
	graph save temp_09b, replace
		
	qui areg pol_status 1.treat07##ib2001.year $firm_cont gender age college if sector2, a(cityID) vce(cluster cityID)
	coefplot, keep(1.treat07#*.year) ///
		coeflabels(1.treat07#2003.year = "2003" ///
		1.treat07#2005.year = "2005" ///
		1.treat07#2007.year = "2007" /// 
		1.treat07#2009.year = "2009" /// 
		1.treat07#2011.year = "2011") ///
		levels(90) ///
		vertical ///
		title(C. Political Status (2007 connection)) ///
		yline(0, lp(dash)) ///
		addplot(line @b @at) ///
		ciopts(recast(rcap)) ///
		graphregion(color(gs16)) ///
		ylabel(#5, nogrid) ///
		scheme(s2mono)
	graph save temp_07a, replace	
		
	qui areg soc_status 1.treat07##ib2001.year $firm_cont gender age college if sector2, a(cityID) vce(cluster cityID)
	coefplot, keep(1.treat07#*.year) ///
		coeflabels(1.treat07#2003.year = "2003" ///
		1.treat07#2005.year = "2005" ///
		1.treat07#2007.year = "2007" /// 
		1.treat07#2009.year = "2009" /// 
		1.treat07#2011.year = "2011") ///
		levels(90) ///
		vertical ///
		title(D. Social Status (2007 connection)) ///
		yline(0, lp(dash)) ///
		addplot(line @b @at) ///
		ciopts(recast(rcap)) ///
		graphregion(color(gs16)) ///
		ylabel(#5, nogrid) ///
		scheme(s2mono)
	graph save temp_07b, replace
	
	graph combine temp_09a.gph temp_09b.gph temp_07a.gph temp_07b.gph, ///
	ysize(12) xsize(18) scheme(sj) graphregion(color(white) icolor(white) fcolor(white))	
	
	capture erase temp_09a.gph
	capture erase temp_09b.gph
	capture erase temp_07a.gph
	capture erase temp_07b.gph

* Table F2 *

	eststo clear
	eststo: qui areg lrev 1.treat09#1.after ib2001.year if sector2, a(cityID) vce(cluster cityID)
	eststo: qui areg lrev 1.treat09#1.after $firm_cont main_ind2 main_ind3 main_ind4 ib2001.year if sector2, a(cityID) vce(cluster cityID)
	eststo: qui areg lrev 1.treat09#1.after $firm_cont main_ind2 main_ind3 main_ind4 $owner_politics gender age college ib2001.year if sector2, a(cityID) vce(cluster cityID)
	eststo: qui areg lrev 1.treat09#1.after 1.treat09#1.after#1.congress $firm_cont main_ind2 main_ind3 main_ind4 $owner_politics gender age college ib2001.year if sector2, a(cityID) vce(cluster cityID)
		lincom 1.treat09#1.after + 1.treat09#1.after#1.congress
	esttab, se b(3) t(3) scalars(N_clust) star(* 0.1 ** 0.05 *** 0.01) keep(1.treat09#1.after 1.treat09#1.after#1.congress)	

* Table G1 *

	eststo clear
	eststo: qui areg pol_status congress $firm_cont gender age college ib2001.year if sector2, a(cityID) vce(cluster cityID)
	eststo: qui areg soc_status congress $firm_cont gender age college ib2001.year if sector2, a(cityID) vce(cluster cityID)
	eststo: qui areg pol_status congress $firm_cont gender age college if sector2 & year == 2011, a(cityID) vce(cluster cityID)
	eststo: qui areg soc_status congress $firm_cont gender age college if sector2 & year == 2011, a(cityID) vce(cluster cityID)
	esttab, se b(3) t(3) scalars(N_clust) star(* 0.1 ** 0.05 *** 0.01) keep(congress)		
